package com.多用户通讯编程.QQ.Server.QQservice;

import com.多用户通讯编程.QQ.Common.Message;
import com.多用户通讯编程.QQ.Common.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class ServerPushThread extends Thread{
    static Scanner sc = new Scanner(System.in);
    @Override
    public void run() {
        System.out.println("推送线程已开启");

        while(true){
            System.out.print("请输入你要推送的消息: ");
            String mess = sc.next();
            Message message = new Message();
            message.setContent(mess);
            message.setSender("服务器");
            message.setMesType(MessageType.MESSAGE_TO_ALL_MES);

            Collection<ServerConnectClientThread> users = ManageServerConnectClientThread.getMap().values();
            Iterator<ServerConnectClientThread> iterator = users.iterator();
            while(iterator.hasNext()){
                /// 遍历拿到每个对象
                ServerConnectClientThread user = iterator.next();
                /// 如果不是发送者自己，就拿到该对象的输出流，并发送；

                ObjectOutputStream oos = user.oos;
                try {
                    oos.writeObject(message);
                    oos.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                
            }


        }
    }
}
